import React from 'react';
import { connect } from 'dva';
import { formatMessage } from 'umi/locale';
import { Input, Modal, TreeSelect, Form, Button } from 'antd';

const { TreeNode } = TreeSelect;
const FormItem = Form.Item;
const { TextArea } = Input;

const ModelClassDetailForm = Form.create()(props => {
  const { modalVisible, form, detail, onCancel, modelClassData } = props;
  // 组件树形控件 子节点渲染
  const renderTreeNodes = data =>
    data.map(item => {
      if (item.children) {
        return (
          <TreeNode title={item.name} key={item.id} value={item.id}>
            {renderTreeNodes(item.children)}
          </TreeNode>
        );
      }
      return <TreeNode title={item.name} key={item.id} value={item.id}></TreeNode>;
    });
  return (
    <Modal
      title={formatMessage({ id: 'app.mm.modelclass.detail' })}
      destroyOnClose
      visible={modalVisible}
      width={618}
      onCancel={onCancel}
      maskClosable={false}
      footer={[
        <Button key="back" size="large" onClick={onCancel}>
          关闭
        </Button>,
      ]}
    >
      <div style={{ width: 530, paddingLeft: 44 }}>
        <FormItem label={formatMessage({ id: 'app.mm.modelclass.info.name' })} colon={false}>
          {form.getFieldDecorator('name', {
            initialValue: detail.name ? detail.name : undefined,
            rules: [
              {
                required: true,
                message: formatMessage({ id: 'app.mm.modelclass.info.name.place' }),
              },
            ],
          })(<Input readOnly />)}
        </FormItem>
        <FormItem label={formatMessage({ id: 'app.mm.modelclass.info.pid' })} colon={false}>
          {form.getFieldDecorator('pid', {
            initialValue: detail.pid && detail.pid !== -1 ? detail.pid : undefined,
          })(
            <TreeSelect
              showSearch
              disabled
              style={{ width: '100%' }}
              dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
              allowClear
              treeDefaultExpandAll
            >
              {renderTreeNodes(modelClassData)}
            </TreeSelect>,
          )}
        </FormItem>
        <FormItem label={formatMessage({ id: 'app.mm.modelclass.info.more.desc' })} colon={false}>
          {form.getFieldDecorator('desc', {
            initialValue: detail.desc ? detail.desc : undefined,
            rules: [],
          })(<TextArea readOnly rows={4} />)}
        </FormItem>
      </div>
    </Modal>
  );
});
export default connect(({ modelclass, loading }) => ({
  modelclass,
  loading: loading.models.modelclass,
}))(ModelClassDetailForm);
